package net.esj.basic.plugins.struts2.interceptor;

import java.util.Collection;

import javax.inject.Inject;

import net.esj.basic.core.engine.factory.bean.BeanFactory;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

//TODO TEST ME!!!
/**
 * interceptor用作插件额外处理
 */
public class ExtraInterceptor extends AbstractInterceptor {

	private BeanFactory beanFactory;
	
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		String url = ServletActionContext.getRequest().getRequestURI();
		
		Collection<PluginInterceptor> interceptors = beanFactory.getBeansByType(PluginInterceptor.class).values();
		for(PluginInterceptor interceptor : interceptors){
			String res = interceptor.plugIntercept(invocation);
			if(!StringUtils.equals(res, invocation.getResultCode())){
				return res;
			}
		}
		return invocation.invoke();
	}

	@Inject
	public void setBeanFactory(BeanFactory beanFactory) {
		this.beanFactory = beanFactory;
	}

	

}
